#!/usr/bin/env python
# -*- coding: utf-8 -*-

from node import Node
from code import Code

""" 構文木メッセージクラス """
class Message(Node):
	def __init__(self, message):
		# TODO: フォーマットチェック
		suffix = message[len(message)-1]
		if suffix == "@":
			self.pagefeed = True
			message = message[:len(message)-1]
		else:
			self.pagefeed = False
		self.length = len(message)
		if self.length > 255:
			raise "Error:"
		self.message = message
	def run(self, writer):
		""" メッセージ命令書き込み
		# [opecode, 改ページフラグ, 文字列の長さ, 文字列]
		# [1,       1,              1,            ?]
		# = 3+?byte
		"""
		writer.writeByte(Code.CODE_MESSAGE)
		if self.pagefeed:
			ff = Code.MSG_FF
		else:
			ff = Code.MSG_NEXT
		writer.writeByte(ff)
		writer.writeByte(self.length)
		writer.writeString(self.message)
		
		writer.writeLog("メッセージ, %d, %d, %s"%(self.pagefeed, self.length, self.message))
		